package com.bigzero.workflow.modules.process.controller;

import com.bigzero.workflow.common.dto.StartProcessDto;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.TableDataInfo;
import com.bigzero.workflow.common.model.TokenInfo;
import com.bigzero.workflow.common.utils.TokenUtil;
import com.bigzero.workflow.modules.process.query.StartQuery;
import com.bigzero.workflow.modules.process.service.HistoryService;
import com.bigzero.workflow.modules.process.service.StartService;
import com.bigzero.workflow.modules.process.vo.HistoryRecordVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程启动
 *
 * @author fanxinxiong
 * @date 2025-02-14 12:07
 **/
@RequestMapping("/process/start")
@RestController
@Tag(name = "与我有关-流程启动")
public class StartController{

    @Autowired
    private StartService startService;

    @Autowired
    private HistoryService historyService;

    /**
     * 我发起的任务列表
     *
     * @param startQuery 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 1)
    @Operation(summary = "我发起的任务分页列表")
    @GetMapping("list")
    public TableDataInfo list(StartQuery startQuery) {
        TokenInfo tokenInfo = TokenUtil.getTokenInfo();
        startQuery.setUserId(tokenInfo.getId());
        return startService.queryPage(startQuery);
    }

    /**
     * 启动流程
     *
     * @param dto 启动流程参数
     * @return 结果
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "启动流程")
    @PostMapping("start")
    public R<String> start(@RequestBody StartProcessDto dto) {
        TokenInfo tokenInfo = TokenUtil.getTokenInfo();
        startService.startProcess(dto, tokenInfo.getId());
        return R.ok();
    }

    /**
     * 查询审批进度
     *
     * @param instanceId 流程实例id
     * @return 审批记录
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "查询审批进度")
    @Parameters({
        @Parameter(name = "instanceId", description = "流程实例id", required = true)
    })
    @GetMapping("getHistoryRecord")
    public R<List<HistoryRecordVo>> getHistoryRecord(String instanceId) {
        List<HistoryRecordVo> list = historyService.getHistoryRecord(instanceId);
        return R.ok(list);
    }

    /**
     * 查询流程图信息(高亮信息)
     *
     * @param instanceId 流程实例id
     * @return 流程图信息
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 4)
    @Operation(summary = "查询流程图信息(高亮信息)")
    @Parameters({
            @Parameter(name = "instanceId", description = "流程实例id", required = true)
    })
    @GetMapping("getHighlightNodeInfo")
    public R<Map<String, Object>> getHighlightNodeInfo(String instanceId) {
        Map<String, Object> result = historyService.getHighlightNodeInfo(instanceId);
        return R.ok(result);
    }

    /**
     * 获取主表单信息
     *
     * @param instanceId 流程实例id
     * @return 主表单数据
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 5)
    @Operation(summary = "获取主表单信息")
    @Parameters({
            @Parameter(name = "instanceId", description = "流程实例id", required = true)
    })
    @GetMapping("getMainFormInfo")
    public R<Map<String,Object>> getMainFormInfo(String instanceId) {
        Map<String,Object> list = historyService.getMainFormInfo(instanceId);
        return R.ok(list);
    }

    /**
     * 删除流程实例
     *
     * @param instanceId 流程实例id
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 6)
    @Operation(summary = "删除流程实例")
    @DeleteMapping("delete")
    public R<String> delete(@RequestBody String instanceId) {
        startService.delete(instanceId);
        return R.ok();
    }
}
